<?php

namespace Web\MainSiteBundle\Controller;

use Model\SchoolBundle\Model\SchoolModel;
use Model\TeacherBundle\Model\TeacherApplyModel;
use Model\TeacherBundle\Model\TeacherModel;
use Model\TeacherCategoryBundle\Model\TeacherCategoryModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;

/**
 * @route("/Teacher/")
 */
class TeacherApplyController extends WebBaseController
{
    /**
     * @route("add",name="WebTeacherApply")
     */
    public function WebTeacherApply(Request $request)
    {
        $user = $this->needToBeUser();
        $model = new TeacherModel($this->getDoctrine());
        if ( $request->getMethod() == "POST") {//新增
            //获取教师信息
            if (!$category = $this->getParamRequired("category")) {
                return $this->js_out("教师职称未选择");
            }
            if (!$name = $this->getParamRequired("name")) {
                return $this->js_out("请填写教师名");
            }
            if (!$intro = $this->getParamRequired("intro")) {
                return $this->js_out("个人简介未设置");
            }

            if (!$idCode = $this->getParamRequired("idCode")) {
                return $this->js_out("身份证号为必填信息");
            } else {
                $params["idCode"] = $idCode;
            }
            if (!$idPic1 = $this->getParamRequired("idPic1")) {
                return $this->js_out("请上传身份证正面照片");
            } else {
                $params["idPic1"] = $idPic1;
            }
            if (!$idPic2 = $this->getParamRequired("idPic2")) {
                return $this->js_out("请上传身份证被面照片");
            } else {
                $params["idPic2"] = $idPic2;
            }
            if ($teacherPic = $this->getParamRequired("teacherPic")) {
                $params["teacherPic"] = $teacherPic;
            }
            $params = array(
                "userId"        => $user->getId(),
                "category"      => $category,//职称
                "name"          => $name,
                "intro"         => $intro,
                "addTime"       => time(),
                "params"        => json_encode($params),//Json 字符串
                "status"        => 0,//0=未审核，1=已审核
            );
            $rst = $model->addNewApply($params);
            if ($rst) {
                return $this->js_out("信息已提交请耐心等待");
            } else {
                return $this->js_out("返回异常信息");
            }
        } else {
            $teacherCategoryModel = new TeacherCategoryModel($this->getDoctrine());
            $teacherCategory = $teacherCategoryModel->getAllTeacherCategory();
            $data["cat"] = $teacherCategory;

            $hasTeacher = $model->checkHasTeacher($user->getId());
            if (!empty($hasTeacher)) {//已经有创建过教师
                return $this->js_out("您已成为教师，请勿重复申请",$this->generateUrl("WebUserIndex"));
            } else {//暂时没有教师
                //分两种情况：1.未申请；2.申请了但是没通过
                $teacherApplyModel = new TeacherApplyModel($this->getDoctrine());
                $apply = $teacherApplyModel->getByUserId($user->getId());
                if ( isset($apply["params"]) && isset($apply["status"]) ) {//申请已经提交
                    $params = json_decode($apply["params"],true);
                    if ($apply["status"] == 0) {
                        $msg = "您的信息已提交，工作人员将尽快审核，再次提交将覆上次提交的内容。";
                    } else {
                        $msg = isset($params["msg"]) ? $params["msg"] : "您提交的信息可能存在问题，建议您重新提交";
                    }
                    $data["msg"] = $msg;
                }
                return $this->render("WebMainSiteBundle:User:user_apply_teacher.html.twig",$data);
            }
        }
    }
}
